package com.cyberypower.sjs.jabber;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JabberAddress {

    private String user;
    private String pass;
    private String host;


    public JabberAddress(String user, String pass, String host) {
        this.user = user;
        this.pass = pass;
        this.host = host;
    }

    public JabberAddress(String jid) {
        Pattern jidPassPattern = Pattern.compile("(.*):(.*)@(.*)");
        Pattern jidPattern = Pattern.compile("(.*)@(.*)");
        Matcher matcher = jidPassPattern.matcher(jid);
        if (matcher.find()) {
            user = matcher.group(1);
            pass = matcher.group(2);
            host = matcher.group(3);
        } else {
            matcher = jidPattern.matcher(jid);
            if (matcher.find()) {
                user = matcher.group(1);
                host = matcher.group(2);
            } else
                JabberException.deadlock();
        }
    }

    public String getUser() {
        return user;
    }

    public String getHost() {
        return host;
    }


    public String getPass() {
        return pass;
    }

    public String toString() {
        return user + "@" + host;
    }


    public boolean equals(Object obj) {
        JabberAddress ja = (JabberAddress) obj;
        return (host.equals(ja.getHost()) && user.equals(ja.getUser()));
    }
}
